import {createRouter, createWebHistory, createWebHashHistory} from "vue-router";
import store from "@/store/index.js";
import api from "@/request/index.js";

const router =[
    {
      path:"/",
      redirect:"/home",
      meta:{title:'首页'}
    },
    {
        path:"/home",
        name:"home",
        component: ()=>import("@/components/home/Home.vue"),
        meta:{title:'首页'},
    },
    {
        path:"/articleDetails",
        name:"articleDetails",
        component: ()=>import("@/components/detail/ArticleDetails.vue"),
        meta:{title:'详情'},
    },
    {
        path:"/game",
        name:"game",
        component: ()=>import("@/components/game/game.vue"),
        meta:{title:'游戏'},
    },
    {
        path:"/announcement",
        name:"announcement",
        component: ()=>import("@/components/announcement/Announcement.vue"),
        meta:{title:'公告'},
    },
    {
        path:"/software",
        name:"software",
        component: ()=>import("@/components/software/SoftWare.vue"),
        meta:{title:'软件'},
    },
    {
        path:"/websiteHelp",
        name:"websiteHelp",
        component: ()=>import("@/components/websiteHelp/WebsiteHelp.vue"),
        meta:{title:'帮助'},
    },
    {
        path:"/edit",
        name:"edit",
        component: ()=>import("@/components/detail/Edit.vue"),
        meta:{title:'帮助'},
    },
    {
        path:"/search",
        name:"search",
        component: ()=>import("@/components/search/Search.vue"),
        meta:{title:'搜索'},
    }

    // {
    //     path: "/resource",
    //     name:"resource",
    //     component: ()=>import("@/components/home/HomeResource.vue"),
    //     redirect: "/resource/resourceCenter",
    //     children:[
    //         {
    //             path: "/resourceCenter",
    //             name:"resourceCenter",
    //             component: ()=>import("@/components/home/HomeResourceContent.vue")
    //         },
    //     ]
    // }

];

const routers = createRouter({
    //历史模式
    history: createWebHashHistory(),
    routes: router,

})

//全局导航守卫(切换路由前)
routers.beforeEach((to,from, next)=>{
    if(to.path.indexOf("home")!==-1){
        window.localStorage.setItem("Current_option","0");
    }else if(to.path.indexOf("game")!==-1){
        window.localStorage.setItem("Current_option","1");
    }else if(to.path.indexOf("software")!==-1){
        window.localStorage.setItem("Current_option","2");
    }else if(to.path.indexOf("announcement")!==-1 || to.path.indexOf("websiteHelp")!==-1){
        window.localStorage.setItem("Current_option","-1");
    }else if(to.path.indexOf("articleDetails")!==-1){
        window.localStorage.setItem("Current_option","1000");
    }

    store.commit('headerLoad');
    next();
});

//全局导航守卫(切换路由后)
routers.afterEach((to,from)=>{
    //如果登录,获取是否有评论信息
    if(window.localStorage.getItem("AUTHORIZATION_TOKEN")){
        api.getCommentSize().then((res)=>{
            if(res.data.code===200){
                store.commit('commentsSize',res.data.data)
            }
        })
    }
    //如果是前往文章详情，文章浏览加一
    if(to.path === "/articleDetails"){
        //从路径中获取参数
        api.articleVisit(to.query.id).then();
    }else {
        document.title = to.meta.title || '默认名'    //修改网页的title
    }
    window.scroll(0,0); //回到页面顶部
});

//到处路由
export default routers;








